﻿package{
	import com.ecity.events.MenuEvent;
	import com.ecity.resource.Resource;
	import com.ecity.ui.CategoryMenu;
	import com.greensock.TweenLite;
	import flash.display.Sprite;
	import flash.display.GradientType;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	public class Navigation extends Sprite{
		protected var WIDTH:int = 283;
		protected var HEIGHT:int = 600;
		protected var BG_COLORS:Array = [0x0a142d,0x18327f];
		protected var ALPHAS:Array = [100, 100];
		protected var RATIOS:Array = [0, 255];
		protected var matrix:Matrix = new Matrix();
		
		private var cates:Array;
		private var movies:CategoryMenu;
		
		public function Navigation() {
			new Resource();
			cates = new Array();
			matrix.createGradientBox( HEIGHT, WIDTH, -Math.PI/3);
			initBackground();
			addCategoryMenus();
		}
		
		private function addCategoryMenus():void {
			for (var i:int = 0; i < 4; i++) {
				var cate:CategoryMenu= new CategoryMenu(i);
				this.addChild(cate);
				cates.push(cate);
				cate.addSubMemu(4);
				cate.addEventListener(MenuEvent.CATE_CHANGED, onCategoryChanged);
				cate.addEventListener(MenuEvent.CATE_MENU_CHANGED, onCategoryMenuChanged);
			}
		}
		
		private function onCategoryChanged(e:MenuEvent):void {
			var selected:CategoryMenu = e.target as CategoryMenu;
			update(selected.index);
		}
		private function onCategoryMenuChanged(e:MenuEvent):void {
			var selected:CategoryMenu = e.target as CategoryMenu;
			trace(e.menuHeight+">>>>>>>>");
			update(selected.index,e.menuHeight);
		}
		
		
		private function update(selectedIndex:int,subHeight:int=0):void {
			var beSelected:CategoryMenu = cates[selectedIndex] as CategoryMenu;
			TweenLite.to(beSelected, 0.2 ,{y:selectedIndex* 50,delay:0.5} );
			for (var i:int = 0; i < 4; i++) {
				var cate:CategoryMenu = cates[i] as CategoryMenu;
				if (cate.index != selectedIndex && cate.isOpend) {
					cate.pickup();
				}
				if (cate.index > selectedIndex) {
					TweenLite.to(cate, 0.2 ,{y:(i+1)* 50+(beSelected.count-1)*38+subHeight,delay:0} );
				}else if(cate.index < selectedIndex) {
					TweenLite.to(cate, 0.2 , { y:i * 50, delay:0} );
				}
			}
		}
		
		private function initBackground():void {
			this.graphics.beginGradientFill(GradientType.LINEAR, BG_COLORS, ALPHAS, RATIOS,matrix);
			this.graphics.drawRect(0, 0, WIDTH, HEIGHT);
			this.graphics.endFill();
		}
		
	}
}